﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;

namespace DTcms.DAL
{
    /// <summary>
	/// 数据访问类:ch_view_log
	/// </summary>
    public partial class view_log
    {
        public view_log()
        { }

        #region 基本方法============================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_view_log");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.view_log model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ch_view_log(");
            strSql.Append("type,user_id,user_name,view_id,view_title,con,add_time)");
            strSql.Append(" values (");
            strSql.Append("@type,@user_id,@user_name,@view_id,@view_title,@con,@add_time)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@type", SqlDbType.Int,4),
                    new SqlParameter("@user_id", SqlDbType.Int),
                    new SqlParameter("@user_name", SqlDbType.NVarChar,100),
                    new SqlParameter("@view_id", SqlDbType.Int),
                    new SqlParameter("@view_title", SqlDbType.NVarChar,100),
                    new SqlParameter("@con", SqlDbType.NVarChar,255),
                    new SqlParameter("@add_time", SqlDbType.DateTime)};

            parameters[0].Value = model.type;
            parameters[1].Value = model.user_id;
            parameters[2].Value = model.user_name;
            parameters[3].Value = model.view_id;
            parameters[4].Value = model.view_title;
            parameters[5].Value = model.con;
            parameters[6].Value = model.add_time;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.view_log model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_view_log set ");
            //type,user_id,user_name,view_id,view_title,con,add_time
            strSql.Append("type = @type,");
            strSql.Append("user_id = @user_id,");
            strSql.Append("user_name = @user_name,");
            strSql.Append("view_id = @view_id,");
            strSql.Append("view_title = @view_title,");
            strSql.Append("con = @con,");
            strSql.Append("add_time = @add_time");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,4) ,
                          new SqlParameter("@user_id", SqlDbType.Int,50),
                    new SqlParameter("@user_name", SqlDbType.NVarChar,50),
                    new SqlParameter("@view_id", SqlDbType.Int,50),
                    new SqlParameter("@view_title", SqlDbType.NVarChar,50),
                    new SqlParameter("@con", SqlDbType.NVarChar,50),
                    new SqlParameter("@add_time", SqlDbType.DateTime)
        };
            parameters[0].Value = model.id;
            parameters[1].Value = model.type;
            parameters[2].Value = model.user_id;
            parameters[3].Value = model.user_name;
            parameters[4].Value = model.view_id;
            parameters[5].Value = model.view_title;
            parameters[6].Value = model.con;
            parameters[7].Value = model.add_time;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_view_log ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.view_log GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,type,user_id,user_name,view_id,view_title,con,add_time");
            strSql.Append(" from ch_view_log");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.view_log model = new Model.view_log();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,type,user_id,user_name,view_id,view_title,con,add_time");
            strSql.Append(" FROM ch_view_log ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ch_view_log");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion


        #region 扩展方法============================
    
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.view_log DataRowToModel(DataRow row)
        {
            Model.view_log model = new Model.view_log();
            //id,type,user_id,user_name,view_id,view_title,con,add_time
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["type"] != null)
                {
                    model.type = int.Parse(row["title"].ToString());
                }
                if (row["user_id"] != null && row["user_id"].ToString() != "")
                {
                    model.user_id = int.Parse(row["user_id"].ToString());
                }

                if (row["user_name"] != null && row["user_name"].ToString() != "")
                {
                    model.user_name = row["user_name"].ToString();
                }

                if (row["view_id"] != null && row["view_id"].ToString() != "")
                {
                    model.view_id = int.Parse(row["view_id"].ToString());
                }

                if (row["view_title"] != null && row["view_title"].ToString() != "")
                {
                    model.view_title = row["view_title"].ToString();
                }
                if (row["con"] != null && row["con"].ToString() != "")
                {
                    model.con = row["con"].ToString();
                }

                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time =DateTime.Parse( row["add_time"].ToString());
                }


            }
            return model;
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int user_id,int xunjia_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_view_log");
            strSql.Append(" where user_id=@user_id");
            strSql.Append(" and view_id=@view_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@user_id", SqlDbType.Int,4),
                     new SqlParameter("@view_id", SqlDbType.Int, 4)};
            parameters[0].Value = user_id;
            parameters[1].Value = xunjia_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        #endregion
    }
}
